package com.hzcx.app.simplechat.ui.group;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.chat.bean.GroupSearchBean;
import com.hzcx.app.simplechat.ui.group.bean.GroupJoinBean;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.toast.ToastUtils;

public class AddGroupForSearchActivity extends AppCompatActivity {

    public Fragment addDroupInfo;
    public Fragment addGroupQuestion;
    public Fragment addGroupSentResult;

    public static String bean = "GroupSearchBean";
    public GroupSearchBean groupSearchBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_group_for_search);


        // 动态添加fragment   需要在XML中打开FragmentLayout
//        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//        groupInfoFragment = new GroupInfoFragment();
//        transaction.add(R.id.add_group_frame_layout, groupInfoFragment);
////        transaction.add(R.id.frame_layout, homeFragment);
//        transaction.commit();

        groupSearchBean = (GroupSearchBean)getIntent().getSerializableExtra(bean);
//        GroupInfoFragment groupInfoFragment = getFragmentManager().getFragments().get(0);
//        groupInfoFragment.groupSearchBean = bean;

        selectFragment(0);
    }


     public void selectFragment(int i) {

        //获取FragmentManager对象
        FragmentManager manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        hideFragments(transaction);

        switch (i) {
            case 0: {
                if (addDroupInfo != null) {
                    transaction.show(addDroupInfo);
                } else {
                    addDroupInfo = new GroupInfoFragment();
                    transaction.add(R.id.fragment_group_search_add, addDroupInfo);
                }
            }
            break;
            case 1: {
                if (addGroupQuestion != null) {
                    transaction.show(addGroupQuestion);
                } else {
                    addGroupQuestion = new GroupQuestionFragment();
                    transaction.add(R.id.fragment_group_search_add, addGroupQuestion);
                }
            };
            break;
            case 2: {
                if (addGroupSentResult != null) {
                    transaction.show(addGroupSentResult);
                } else {
                    addGroupSentResult = new GroupSentResultFragment();
                    transaction.add(R.id.fragment_group_search_add, addGroupSentResult);
                }
            };
            break;
        }
        transaction.commit();
    }

    //将四个的Fragment隐藏
    private void hideFragments(FragmentTransaction transaction){
        if(addDroupInfo != null){
            transaction.hide(addDroupInfo);
        }
        if(addGroupQuestion != null){
            transaction.hide(addGroupQuestion);
        }
        if(addGroupSentResult != null){
            transaction.hide(addGroupSentResult);
        }
    }

    public void applyJoinGroup(@Nullable String answer) {
        answer = answer.isEmpty() ? "" : answer;
        String question = groupSearchBean.getQuestion1();
        Log.e("申请加群", "getCustom_number: " + groupSearchBean.getHx_groupid() + ", question: " + question + ", answer: " + answer);
        GroupModel.applyJoin(this, groupSearchBean.getGroup_id(), question, answer, new BaseObserver<GroupJoinBean>() {
            @Override
            protected void onSuccess(GroupJoinBean data) {
                selectFragment(2);
            }
            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
                ToastUtils.show(errorMsg);
                selectFragment(2);
            }
        });
    }

}